/**
 * Copyright (c) 2011-2014, homeant (homeanter@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xin.homeant.upms.client.shiro.session;

import java.io.Serializable;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import xin.homeant.upms.app.service.ISessionService;
import xin.homeant.upms.client.shiro.utils.SerializableUtil;







/**
 * <P>
 * 用户Session存储
 * </P>
 * 
 * @author homeant homeanter@163.com
 * @Data 2017年5月21日 下午4:05:55
 */
public class UpmsSessionDao extends CachingSessionDAO{
	
	private static Logger _log = LoggerFactory.getLogger(UpmsSessionDao.class);
	
	@Autowired
	private ISessionService sessionService;
	
	@Override
	protected void doUpdate(Session session) {
		// 如果会话过期/停止 没必要再更新了
        if(session instanceof ValidatingSession && !((ValidatingSession)session).isValid()) {
            return;
        }
        // 更新session的最后一次访问时间
        UpmsSession upmsSession = (UpmsSession) session;
        UpmsSession cacheUpmsSession = (UpmsSession) doReadSession(session.getId());
        if (null != cacheUpmsSession) {
            upmsSession.setStatus(cacheUpmsSession.getStatus());
            upmsSession.setAttribute("FORCE_LOGOUT", cacheUpmsSession.getAttribute("FORCE_LOGOUT"));
        }
        sessionService.put(upmsSession.getId(),SerializableUtil.serialize(upmsSession));
        _log.debug("doUpdate >>>>> sessionId={}", session.getId());
	}

	@Override
	protected void doDelete(Session session) {
		String sessionId = session.getId().toString();
        // 删除session
		sessionService.delete(sessionId);
        _log.debug("doUpdate >>>>> sessionId={}", sessionId);
	}

	@Override
	protected Serializable doCreate(Session session) {
		Serializable sessionId = generateSessionId(session);
	    assignSessionId(session, sessionId);
	    sessionService.put(session.getId(),SerializableUtil.serialize(session));
	    _log.debug("doCreate >>>>> sessionId={}", sessionId);
        return sessionId;
	}

	@Override
	protected Session doReadSession(Serializable sessionId) {
		return SerializableUtil.deserialize(sessionService.get(sessionId));
	}
	
	/**
     * 更改在线状态
     *
     * @param sessionId
     * @param onlineStatus
     */
    public void updateStatus(Serializable sessionId, UpmsSession.OnlineStatus onlineStatus) {
        UpmsSession session = (UpmsSession) doReadSession(sessionId);
        if (null == session) {
            return;
        }
        session.setStatus(onlineStatus);
        sessionService.put(session.getId(),SerializableUtil.serialize(session));
    }

}
